package controllers

import java.io.{BufferedWriter, File, FileWriter}

import dao._
import implicits.Implicits._
import javax.inject.Inject
import models.Tables._
import org.joda.time.DateTime
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import service._
import tool.Tool
import utils.Utils

import scala.collection.mutable.ArrayBuffer
import scala.collection.parallel.CollectionConverters._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.io.Source

/**
 * Created by Administrator on 2019/8/14
 */
class TestController @Inject()(cc: ControllerComponents, junctionDao: JunctionDao,
                               fusionSymbolsService: FusionSymbolsService, modalSymbolsService: ModalSymbolsService
                               ) extends AbstractController(cc) {

  def test = Action.async { implicit request =>
    val startTime = System.currentTimeMillis()
    junctionDao.selectAll.map { case ts =>
      println(Utils.getTime(startTime))
      val idLocationMap = ts.map(x => (x._1, x._2)).toMap
      Utils.logTime(startTime)
      val parent = if (Utils.isWindows) {
        new File("C:\\rJunBase_database\\test\\pancancer_plot")
      } else new File(Tool.path, "/test/pancancer_plot")
      val dbDir = if (Utils.isWindows) {
        new File("E:\\Rjunbase\\detail\\expr_format\\pancancer")
      } else {
        new File("/mnt/sdb/zhengzq/RJunBase/detail/expr_format/pancancer")
      }
      dbDir.listFiles().foreach { file =>
        val kind = file.getName.split("_")(0)
        val outFile = new File(parent, s"${kind}.csv")
        if (outFile.nonExist) {
          val bw = new BufferedWriter(new FileWriter(outFile), 16384)
          val header = List("sample", "value").mkString(",")
          bw.write(s"${header}\n")
          println(file.getName)
          val lines = file.csvLines.map(_.toArray)
          Utils.logTime(startTime)
          val locationIndexs = lines.head.zipWithIndex
          println(Utils.getTime(startTime))
          var count = 0
          locationIndexs.drop(3).foreach { case (location, index) =>
            count += 1
            if (count % 10000 == 0) {
              println(Utils.getTime(startTime), count)
            }
            val array = lines.drop(1).map { columns =>
              Map("class" -> columns(1), "value" -> columns(index))
            }
            val line = List(location, Json.toJson(array).toString()).mkString(",")
            bw.write(line + "\n")
          }
          bw.close()
        }


      }

      //      dbDir.myListFiles.foreach { file =>
      //        println(file.getName)
      //        val outFile = new File(parent, file.getName)
      //        val bw = new BufferedWriter(new FileWriter(outFile), 16384)
      //        //      outDir.deleteIfExist.createDirectoryWhenNoExist
      //        val lines=file.csvLines
      //        Utils.logTime(startTime)
      //        val maps=
      //        val rawHeaders = lines.next().mySplit(",")
      //        bw.write(List("sample","class","cancer","location","value").mkString(",") + "\n")
      //        lines.foreach{line=>
      //          val columns=line.mySplit(",")
      //          rawHeaders.drop(3).zipWithIndex.foreach { case (location, i) =>
      //            List(columns(0))
      //              bw.write(""+"\n")
      //              val contentLines = maps.map { map =>
      //                mapHeader.map { header =>
      //                  map(header)
      //                }
      //              }
      //              val newLines = header :: contentLines
      //              newLines.toFile(outFile)
      //            if (i % 50000 == 0) {
      //              println(i, Utils.getTime(startTime))
      //            }
      //          }
      //        }
      //
      //
      //      }
      println(Utils.logTime(startTime))
      Ok("success!")
    }

  }


}
